package com.newbingo.dto;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.newbingo.dao.imple.IAgentDao;
import com.newbingo.dto.imple.IAgentDto;
import com.newbingo.entity.Agent;
import com.newbingo.service.imple.IAgentService;
import com.newbingo.util.AgentTree;
import com.newbingo.util.Pager;
import org.apache.struts2.ServletActionContext;
import javax.servlet.http.*;

public class AgentDto extends UserDto implements IAgentDto, Serializable {
	private Integer agentType;
	private Integer parentId;
	private IAgentDao agentDao;
	
	private IAgentService agentService=null;

	/*
	 * 
	 */
	public Pager findSubAgents(int start, int limit, Integer agentId,
			Integer agentType) {
		Pager pageDto = new Pager();
		Pager page_agents = agentDao.findSubAgents(start, limit, agentId,
				agentType);
		pageDto.setTotalSize(page_agents.getTotalSize());
		List<Agent> agents = page_agents.getData();
		// 循环出Agent对象，并进行转换成DTO对象保存到pageDto中
		for (Agent agent : agents) {
			pageDto.getData().add(changeToDto(agent));
		}
		return pageDto;
	}

	/*
	 * 根据代理ID查询并返回DTO对象
	 */
	public AgentDto findById(Integer id) {
		Agent agent = agentDao.findById(id);
		return changeToDto(agent);
	}

	/*
	 * 根据登录名和密码进行查询,返回DTO对象
	 */
	public AgentDto findByLogin(String uname, String pwd) {
		// return changeToDto(agentDao.findByLogin(uname, pwd));
		HttpServletRequest request = ServletActionContext.getRequest();
		String ip = request.getRemoteAddr();
		Agent agent = agentDao.findByLogin(uname, pwd);
		if (agent != null) {
			agent.setLastLoginIp(ip);
			agent.setLastLogTime(new Date());
			agentService.merge(agent);
			return changeToDto(agent);
		} else {
			return null;
		}
	}

	/*
	 * 将Agent对象转换为AgentDto对象
	 */
	private AgentDto changeToDto(Agent agent) {
		AgentDto agentDto = new AgentDto();
		agentDto.setAgentType(agent.getAgentType());
		if (agent.getContactInfo() != null) {
			agentDto.setEmail(agent.getContactInfo().getEmail());
			agentDto.setTel(agent.getContactInfo().getTel());
		}
		agentDto.setStatus(agent.getStatus());
		agentDto.setTrueName(agent.getTrueName());
		agentDto.setUname(agent.getUname());
		agentDto.setUno(agent.getUno());
		agentDto.setBaseAmount(agent.getBaseAmount());
		agentDto.setBalance(agent.getBalance());
		if (agent.getRegTime() != null) {
			agentDto.setRegTime(agent.getRegTime().toString());
		} else {
			agentDto.setRegTime("");
		}
		if (agent.getLastLogTime() != null) {
			agentDto.setLastLoginTime(agent.getLastLogTime().toString());
		} else {
			agentDto.setLastLoginTime("");
		}
		agentDto.setLastLoginIp(agent.getLastLoginIp());
		if (agent.getParentAgent() != null)
			agentDto.setParentId(agent.getParentAgent().getUno());
		return agentDto;
	}

	public Integer getAgentType() {
		return agentType;
	}

	public void setAgentType(Integer agentType) {
		this.agentType = agentType;
	}

	public IAgentDao getAgentDao() {
		return agentDao;
	}

	public void setAgentDao(IAgentDao agentDao) {
		this.agentDao = agentDao;
	}

	public Integer getParentId() {
		return parentId;
	}

	public void setParentId(Integer parentId) {
		this.parentId = parentId;
	}

	public IAgentService getAgentService() {
		return agentService;
	}

	public void setAgentService(IAgentService agentService) {
		this.agentService = agentService;
	}

}
